using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Log;
using System.Xml;
using System.Xml.Schema;

namespace SoinusLibrary.Log
{
    public class LogManager
    {
        #region Fields
        private XmlSchema _xmlSchema;
        private static LogManager _instance;
        #endregion

        #region Properties
        public static LogManager Instance
        {
            get
            {
                if (_instance == null) _instance = new LogManager();
                return _instance;
            }
        }  
        #endregion
        
        #region Constructor
        private LogManager()
        {
            //Xml Schema
            Stream str = new MemoryStream();
            TextWriter textWriter = new StreamWriter(str);
            TextReader textReader = new StreamReader(str);
            textWriter.Write(Log.Resources.LogSchema.ToString());
            textWriter.Flush();
            str.Position = 0;
            _xmlSchema = XmlSchema.Read(str, ValidationCallback);
        } 
        #endregion

        #region Methods
        static void ValidationCallback(object sender, ValidationEventArgs args)
        {
            if (args.Severity == XmlSeverityType.Warning)
                Console.Write("WARNING: ");
            else if (args.Severity == XmlSeverityType.Error)
                Console.Write("ERROR: ");

            Console.WriteLine(args.Message);
        }

        public void WriteLog(LogEntry log)
        {
            XmlDocument xmlDoc = new XmlDocument();

            XmlElement root;
            XmlElement logEntry;
            XmlAttribute date;
            XmlAttribute location;
            XmlAttribute message;

            if (!File.Exists("Log.xml"))
            {
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                root = xmlDoc.DocumentElement;
                xmlDoc.InsertBefore(xmlDeclaration, root);
                root = xmlDoc.CreateElement("Logs");
                xmlDoc.AppendChild(root);
            }
            else
            {
                xmlDoc.Load("Log.xml");
            }

            root = xmlDoc.DocumentElement;

            logEntry = xmlDoc.CreateElement("LogEntry");

            date = xmlDoc.CreateAttribute("date");
            date.Value = log.Date.ToString();

            location = xmlDoc.CreateAttribute("location");
            location.Value = log.Location;

            message = xmlDoc.CreateAttribute("message");
            message.Value = log.Message;

            logEntry.Attributes.Append(date);
            logEntry.Attributes.Append(location);
            logEntry.Attributes.Append(message);

            root.AppendChild(logEntry);

            xmlDoc.Save("Log.xml");
        }

        public void WriteLog(DateTime date, String location, String message)
        {
            WriteLog(new LogEntry(date, location, message));
        } 
        #endregion
    }
}
